home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CD Classic
/
CD CLASSIC #1.iso
/
chess
/
chess.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-08-15
|
27KB
|
815 lines
/*
C source for GNU CHESS
Revision: 1991-01-20
Modified by Daryl Baker for use in MS WINDOWS environment
Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
Copyright (c) 1988, 1989, 1990 John Stanback
This file is part of CHESS.
CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY. No author or distributor accepts responsibility to anyone for
the consequences of using it or for whether it serves any particular
purpose or works at all, unless he says so in writing. Refer to the CHESS
General Public License for full details.
Everyone is granted permission to copy, modify and redistribute CHESS, but
only under the conditions described in the CHESS General Public License.
A copy of this license is supposed to have been given to you along with
CHESS so you can know your rights and responsibilities. It should be in a
file named COPYING. Among other things, the copyright notice and this
notice must be preserved on all copies.
*/
#define NOATOM /*Minimize windows.h processing*/
#define NOCLIPBOARD
#define NOCREATESTRUCT
#define NOFONT
#define NOREGION
#define NOSOUND
#define NOWH
#define NOCOMM
#define NOKANJI
#include <windows.h> /* required for all Windows applications */
#include <string.h>
#include <time.h>
#include "gnuchess.h"
#include "defs.h"
#include "chess.h"
#include "saveopen.h"
#include "color.h"
DWORD clrBackGround; /* rgb structures for various colors */
DWORD clrBlackSquare;
DWORD clrWhiteSquare;
DWORD clrBlackPiece;
DWORD clrWhitePiece;
DWORD clrText;
static HBRUSH hBrushBackGround;
short boarddraw[64]; /* Display copies of the board */
short colordraw[64]; /* Needed because while computer is calculating*/
/* moves it updates board and color thus you can*/
/* not repaint the screen accuratly */
struct PIECEBITMAP pieces[7];
HANDLE hInst; /* current instance */
HANDLE hAccel;
char szAppName[] = "Chess";
extern char mvstr[4][6];
extern GLOBALHANDLE hBook;
int coords = 1;
static int FirstSq = -1; /* Flag is a square is selected */
static int GotFirst = FALSE;
static int EditActive = FALSE; /* Edit mode? */
static int User_Move = TRUE; /* User or computer's turn */
static HMENU hMainMenu;
int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine,
int nCmdShow);
static void _near MakeHelpPathName (char * szFileName);
#define EXE_NAME_MAX_SIZE 128
int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
MSG msg;
POINT pt;
lpCmdLine ++;
if (!hPrevInstance)
if (!ChessInit(hInstance)) return (NULL);
hInst = hInstance; /* Saves the current instance */
QueryBoardSize (&pt);
/* Create the mailn window. It will be autosized in WM_CREATE message */
hWnd = CreateWindow(szAppName, szAppName,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if (!hWnd) return (NULL);
ShowWindow(hWnd, nCmdShow); /* Shows the window */
/* Initialize chess */
if (init_main ( hWnd ) ) {
SMessageBox (hWnd, IDS_INITERROR,IDS_CHESS);
FreeGlobals ();
return (NULL);
}
UpdateWindow(hWnd); /* Sends WM_PAINT message */
hAccel = LoadAccelerators (hInstance, szAppName);
player = opponent;
ShowSidetoMove ();
while (GetMessage(&msg, NULL, NULL, NULL)) {
if ( !TranslateAccelerator (hWnd, hAccel, &msg) ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (msg.wParam); /* Returns the value from PostQuitMessage */
}
long FAR PASCAL ChessWndProc( HWND hWnd, unsigned message, WORD wParam, LONG lParam)
{
FARPROC lpProcAbout;
HMENU hMenu;
PAINTSTRUCT ps;
HDC hDC;
TEXTMETRIC tm;
int i;
POINT point;
OFSTRUCT pof;
char FileName[256], str[80];
int Status;
HANDLE hFile;
switch (message) {
case WM_CREATE: { /* message: window being created */
int xchar, ychar;
GetStartupColors ( szAppName);
hBrushBackGround = CreateSolidBrush ( clrBackGround );
for ( i=pawn; i<pawn+6; i++ ) pieces[i].piece = LoadBitmap (hInst, MAKEINTRESOURCE(PAWNBASE+i));
for ( i=pawn; i<pawn+6; i++ ) pieces[i].mask = LoadBitmap (hInst, MAKEINTRESOURCE(PAWNBASE+6+i));
for ( i=pawn; i<pawn+6; i++ ) pieces[i].outline = LoadBitmap (hInst, MAKEINTRESOURCE(PAWNBASE+12+i));
hDC = GetDC (hWnd);
GetTextMetrics ( hDC, &tm);
xchar = tm.tmMaxCharWidth;
ychar = tm.tmHeight+tm.tmExternalLeading;
/*Autosize main window */
QueryBoardSize (&point);
SetWindowPos( hWnd, hWnd, 0,0,
point.x+GetSystemMetrics(SM_CXFRAME)*2+50,
point.y+GetSystemMetrics(SM_CYFRAME)*2+GetSystemMetrics(SM_CYMENU)+
GetSystemMetrics(SM_CYCAPTION) + ychar,
SWP_NOMOVE | SWP_NOZORDER);
ReleaseDC ( hWnd, hDC);
InitHitTest ();
Create_Children ( hWnd, hInst, xchar, ychar);
break;
}
case MSG_DESTROY:
DestroyWindow ( hWnd );
break;
case WM_DESTROY: { /* message: window being destroyed */
char szHelpFileName[EXE_NAME_MAX_SIZE+1];
MakeHelpPathName(szHelpFileName);
WinHelp(hWnd,szHelpFileName,HELP_QUIT,0L);
DeleteObject (hBrushBackGround);
Hittest_Destructor ();
if ( hBook ) FreeBook();
FreeGlobals ();
SaveColors ( szAppName);
PostQuitMessage(0);
break;
}
case WM_PAINT:
if ( FirstSq != -1 ) { /*Properly repaint hilighted square*/
POINT pt; RECT rect;
QuerySqOrigin ( FirstSq%8, FirstSq/8, &pt);
rect.left = pt.x; rect.right=pt.x+48;
rect.top = pt.y-48; rect.bottom = pt.y;
InvalidateRect (hWnd, &rect, FALSE);
}
hDC = BeginPaint ( hWnd, &ps);
Draw_Board ( hDC, flag.reverse, clrBlackSquare, clrWhiteSquare );
if ( coords ) DrawCoords ( hDC, flag.reverse, clrBackGround, clrText);
DrawAllPieces ( hDC, flag.reverse, boarddraw, colordraw, clrBlackPiece, clrWhitePiece );
EndPaint ( hWnd, &ps);
if ( FirstSq != -1 ) HiliteSquare ( hWnd, FirstSq);
break;
case WM_CTLCOLOR: /*Enable setting colors for the message controls*/
{
POINT point;
if ( HIWORD(lParam) == CTLCOLOR_STATIC) {
UnrealizeObject ( hBrushBackGround );
SetBkColor ( wParam, clrBackGround);
SetBkMode ( wParam, TRANSPARENT);
SetTextColor ( wParam, clrText);
point.x = point.y = 0;
ClientToScreen ( hWnd, &point);
SetBrushOrg (wParam, point.x, point.y);
return (DWORD) hBrushBackGround;
} else
return DefWindowProc ( hWnd, message, wParam, lParam);
}
case WM_ERASEBKGND:
{
RECT rect;
UnrealizeObject ( hBrushBackGround);
GetClientRect ( hWnd, &rect);
FillRect ( (HDC)wParam, &rect, hBrushBackGround);
return 1;
}
case WM_INITMENUPOPUP:
if ( !EditActive ) Init_Menus (hWnd, wParam, lParam);
break;
case WM_LBUTTONDOWN:
/* If computer is thinking on human's time stop it at the first
button click. add test to ensure that "human" can't interupt
the computer from thinking through its turn */
if ( User_Move ) {
flag.timeout = true;
flag.bothsides = false;
}
/* Don't continue unle